package com.lixin.smartclassroom.ui.home.tab5.activity

import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.folderlist
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.addDatabaseDialog
import com.lixin.smartclassroom.common.longClickDialog
import com.lixin.smartclassroom.databinding.ActivityDatabaseBinding
import com.lixin.smartclassroom.ui.home.adapter.database
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description 资料库
 * Author: lwc
 * Date:2023/12/11 9:21
 */
class DatabaseAct : BaseActivity<ActivityDatabaseBinding>() {
    override fun getViewBinding() = ActivityDatabaseBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "资料库"
    private lateinit var listBean: ArrayList<DataListBean>

    override fun iniView() {
        binding.recycler.database().apply {
            R.id.rl.onClick {
                start(DatabaseDetailAct::class.java) {
                    putExtra("pid", (models?.get(modelPosition) as DataListBean).id)
                }
            }

            R.id.rl.onLongClick {
                longClickDialog((models?.get(modelPosition) as DataListBean).id!!)
            }

        }

        binding.apply {
            refresh.onRefresh {
                scopeNetLife {
                    listBean = folderlist(index, 10, "0")!!
                    addData(listBean) {
                        if (binding.recycler.bindingAdapter.models?.size == 0) {
                            binding.state.showEmpty()
                            binding.recycler.gone()
                        } else {
                            binding.recycler.visible()
                        }
                        listBean.size == 10
                    }
                }
            }.autoRefresh()
        }


        receiveEvent<String>("refreshFolderList") {
            binding.refresh.refresh()
        }
        receiveEvent<String>("uploadFinish") {
            binding.refresh.refresh()

        }


    }


    override fun onClick() {
        binding.btnAdd.onClick {
            this.addDatabaseDialog()
//            if (binding.recycler.bindingAdapter.checkedCount == 0) {
//                this.addDatabaseDialog("")
//            } else {
//                this.addDatabaseDialog((binding.recycler.bindingAdapter.models?.get(binding.recycler.bindingAdapter.checkedPosition[0]) as DataListBean).id!!)
//
//            }

        }
    }


}